<?php 

//  post 有用 创建
function send_request($url = '', $method = "POST", $data = array(), $header = array(), $type = 1, $synchronous = 1) {
    //控制请求方法范围
    $httpMethod = array('GET', 'POST', 'PUT', 'DELETE');
    $method = strtoupper($method);
    if (!in_array($method, $httpMethod)) {
        return false;
    }
    //请求头初始化
    $request_headers = array();
    $User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
    $request_headers[] = 'User-Agent: '. $User_Agent;
    if($header){
        foreach ($header as $v) {
            $request_headers[] = $v;
        }
    }

    $request_headers[] = 'Accept: text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
    switch ($method) {
        case "POST":
            $request_headers[] = "X-HTTP-Method-Override: POST";
            break;
        case "PUT":
            $request_headers[] = "X-HTTP-Method-Override: PUT";
            break;
        case "DELETE":
            $request_headers[] = "X-HTTP-Method-Override: DELETE";
            break;
        default:
            if($data) {
                //GET请求
                $url = $url . '?' . http_build_query($data);
            }
    }
    //发送http请求
    $ch = curl_init();
    if(!$synchronous) {
        //异步
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);//设置超时时间为1秒,超过1秒则关闭连接
        curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个 cURL 7.10时被加入
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 300); //超时毫秒，cURL 7.16.2中被加入
    }else{
        //curl_setopt($ch, CURLOPT_TIMEOUT, 35); // 最大执行时间
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // 在尝试连接时等待的秒数,等待3s没有连接成功就断开
    }
    curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($ch, CURLOPT_HEADER, 0);// 设置是否显示返回头信息  1返回 0不返回  调试开启
    curl_setopt($ch, CURLOPT_NOBODY, 0); //不想在输出中包含body部分，设置这个选项为一个非零值
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); //在启用CURLOPT_RETURNTRANSFER的时候，返回原生的（Raw）输出。
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https 不进行ssl验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);//设置头信息
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, TRUE);
    }

    if($method != 'GET'){
        //格式化发送数据
        if($data) {
            if(is_array($data)) {
                if ($type == 0) {
                    //supportkb 接口需要
                    $dataValue = http_build_query($data);
                } elseif ($type == 1) {
                    $dataValue = json_encode($data, JSON_UNESCAPED_UNICODE);
                }else{
                    //post 数组
                    $dataValue = $data;
                }
            }else{
                $dataValue = $data;
            }
        }

        if(!empty($dataValue)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dataValue);
        }
    }

    //发送请求获取返回响应
    $result['data'] = curl_exec($ch);
    $result['httpCode'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    if(strlen(curl_error($ch))>1){
        $result = false;
    }

    curl_close($ch);
    return $result;
}

/**
 * [get get请求方法]     有用
 * @param  [type]  $url  [url]
 * @param  boolean $head [请求头]
 * @param  integer $foll [是否自动跳转]
 * @param  boolean $ref  [来路]
 * @return [json]        [返回json数据]
 */
function get($url,$head=false,$foll=1,$ref=false){
    $curl=curl_init();//启动一个CURL会话
    if($head){
        curl_setopt($curl,CURLOPT_HTTPHEADER,$head);//模似请求头
    }
    curl_setopt($curl,CURLOPT_URL,$url);//要访问的地址
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//对认证证书来源的检查
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,1);//从证书中检查SSL加密算法是否存在
    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);//模拟用户使用的浏览器
    @curl_setopt($curl,CURLOPT_FOLLOWLOCATION,$foll);//使用自动跳转
    if($ref){
        curl_setopt($curl,CURLOPT_REFERER,$ref);//带来的Referer
    }else{
        curl_setopt($curl,CURLOPT_AUTOREFERER,1);//自动设置Referer
    }
    curl_setopt($curl,CURLOPT_HTTPGET,1);//发送一个常规的Post请求
    curl_setopt($curl,CURLOPT_COOKIEJAR,$GLOBALS['cookie_file']);//存放Cookie信息的文件名称
    curl_setopt($curl,CURLOPT_COOKIEFILE,$GLOBALS['cookie_file']);//读取上面所储存的Cookie信息
    curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');//解释gzip
    curl_setopt($curl,CURLOPT_TIMEOUT,30);//设置超时限制防止死循环
    curl_setopt($curl,CURLOPT_HEADER,0);//显示返回的Header区域内容
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的形式返回
    $tmpInfo=curl_exec($curl);//执行操作
    // if(curl_errno($curl)) {echo'Errno'.curl_error($curl);}
    // $data[]=curl_getinfo($curl);
    // curl_close($curl);//关键CURL会话
    $data=$tmpInfo;
    return $data;//返回数据
}
/**
 * [post post请求方法]    没用
 * @param  [type]  $url  [url]
 * @param  boolean $head [请求头]
 * @param  integer $foll [是否自动跳转]
 * @param  boolean $ref  [来路]
 * @param  [type]  $post [post提交数据]
 * @return [json]        [返回json数据]
 */
function post($url,$head=false,$foll=1,$ref=false,$post=array()){
    $curl=curl_init();//启动一个CURL会话
    if($head){
        curl_setopt($curl,CURLOPT_HTTPHEADER,$head);//模似请求头
    }
    curl_setopt($curl,CURLOPT_URL,$url);//要访问的地址
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//对认证证书来源的检查
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,1);//从证书中检查SSL加密算法是否存在
    curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);//模拟用户使用的浏览器
    @curl_setopt($curl,CURLOPT_FOLLOWLOCATION,$foll);//使用自动跳转
    if($ref){
        curl_setopt($curl,CURLOPT_REFERER,$ref);//带来的Referer
    }else{
        curl_setopt($curl,CURLOPT_AUTOREFERER,1);//自动设置Referer
    }
    curl_setopt($curl,CURLOPT_POST,1);//发送一个常规的Post请求
    // curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($post));
    curl_setopt($curl,CURLOPT_POSTFIELDS,$post);// Post提交的数据包

    curl_setopt($curl,CURLOPT_COOKIEJAR,$GLOBALS['cookie_file']);//存放Cookie信息的文件名称
    curl_setopt($curl,CURLOPT_COOKIEFILE,$GLOBALS['cookie_file']);//读取上面所储存的Cookie信息
    curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');//解释gzip
    curl_setopt($curl,CURLOPT_TIMEOUT,30);//设置超时限制防止死循环
    curl_setopt($curl,CURLOPT_HEADER,0);//显示返回的Header区域内容
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的形式返回
    $tmpInfo=curl_exec($curl);//执行操作
    // if(curl_errno($curl)) {echo'Errno'.curl_error($curl);}
    // $data[]=curl_getinfo($curl);
    // curl_close($curl);//关键CURL会话
    // $tmpInfo=preg_replace('/script/','js',$tmpInfo);
    $data=$tmpInfo;
    return $data;//返回数据
}

/**
 * [getToken 获取token，返回json具体数据]
 * @param  [type] $appId     [应用id]
 * @param  [type] $secret    [应用密钥]
 * @param  [type] $grantType [授权类型，固定值: client_credentials]
 * @param  string $url       [沙箱环境url]
 * @return [json]            [返回token具体信息]
 */
function getToken($appId,$secret,$grantType,$url="https://smlopenapi.esign.cn"){
    $url = $url."/v1/oauth2/access_token?appId=".$appId."&secret=".$secret."&grantType=".$grantType."";
    $result = get($url);
    return $result;
}


/**
 * [getTokenEnd 只返回token值]
 * @param  [type] $appId     [应用id]
 * @param  [type] $secret    [应用密钥]
 * @param  [type] $grantType [授权类型，固定值: client_credentials]
 * @param  string $url       [沙箱环境url]
 * @return [string]            [返回token具体值]
 */
function getTokenEnd($appId,$secret,$grantType,$url="https://smlopenapi.esign.cn"){
    $url = $url."/v1/oauth2/access_token?appId=".$appId."&secret=".$secret."&grantType=".$grantType."";
    $result = get($url);
    $a = json_decode($result,true);
    $b = $a['data']['token'];
    return $b;
}


// delete 有用
function curlData($url, $data, $header, $method = 'GET', $type='json')
{
    //初始化
    $ch = curl_init();
    if ($type=='json') {
    $headers = ['Content-Type: application/json; charset=utf-8'];
    } elseif ($type=='form-urlencoded') {
    $headers = ['Content-Type: application/x-www-form-urlencoded; charset=utf-8'];
    }
    $headers=array_merge($headers, $header);
    if ($method == 'GET' && $type!='json') {
    if ($data) {
    $querystring = http_build_query($data);
    $url = $url.'?'.$querystring;
    }
    }
    // 请求头，可以传数组
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行后不直接打印出来
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    if ($method == 'POST' && $type=='json') {
    // curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST'); // 请求方式
    curl_setopt($ch, CURLOPT_POST, 1); // post提交
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // post的变量
    } elseif ($method == 'POST' && $type!='json') {
    curl_setopt($ch, CURLOPT_POST, 1); // post提交
    $querystring = http_build_query($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring); // post的变量
    }
    if ($method == 'PUT') {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if ($method == 'DELETE') {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    if (!empty($cookie)) {
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    } //设置cookie
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
    $output = curl_exec($ch); //执行并获取HTML文档内容
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch); //释放curl句柄
    return $output;
}





 ?>